﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using Helper;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Localization;

using SchoolManagementSystem.Objects;
namespace SchoolManagementSystem.Forms
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        public string Username { get; set; }
        public string Password { get; set; }
        string setingpath = Application.StartupPath + "\\Setting.cf";
        string usertag = "/setting/user";
        string languagetag = "/setting/language";
        string storeid = "/setting/storeID";

        public long UserID;
        public long StaffID ;
   
        public string StaffName ;
        public string Emaill;
        public string PassEmail;
        private void frmLogin_Load(object sender, EventArgs e)
        {
            LoadSetting();
        }

        void LoadSetting()
        {
            String user = FileHelper.GetValueFromXml(setingpath, usertag);
            String lang = FileHelper.GetValueFromXml(setingpath, languagetag);
            //GlobalVariable.StoreID = FileHelper.GetValueFromXml(setingpath, storeid);
          //  ValueDefine.StoreID = GlobalVariable.StoreID;
            ckremember.Checked = !String.IsNullOrEmpty(user);
            if(ckremember.Checked)
            {
                txtuser.Text = user;
            }
            if(lang=="VN")
            {
                checkEdit_VN.Checked = true;
                checkEdit_EN.Checked = false;
                SetLanguage(ValueDefine.Language.VN);              
            }
            else
            {
                checkEdit_VN.Checked = false;
                checkEdit_EN.Checked = true;
                SetLanguage(ValueDefine.Language.EN);
            }
        }

        bool IsRequired()
        {
            bool result = false;
            if (String.IsNullOrEmpty(txtuser.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired1);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired1);
            }
            if (String.IsNullOrEmpty(txtpass.Text))
            {
                result = true;
                ValidateControls.Required(lblrequired2);
            }
            else
            {
                result = false;
                ValidateControls.NoneRequired(lblrequired2);
            }

           
            return result;
        }

        private void cmdlogin_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void cmdexit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cmdlogin_Click(sender, e);
        }


        void Login()
        {
            try
            {
                string mess = null;
                if (IsRequired())
                    return;
                Username = txtuser.Text;
                Password = txtpass.Text;
                StaffID = 0;
                StaffName = "";
                UserID = 0;
                frmMain.GroupModuleRole = RegisterService.RegisterServices.UserService.GetGroupModuleRole(this.Username, this.Password, ref mess, ref UserID, ref StaffID, ref StaffName);
                frmMain.UserModuleRole = RegisterService.RegisterServices.UserService.GetUserModuleRole(this.Username, this.Password, ref mess, ref UserID, ref StaffID, ref StaffName);

                if (!String.IsNullOrEmpty(mess))
                {
                    MessageControl.ShowMessage(mess, "Thông báo", MessageControl.Icon.Warning);
                    return;
                }

                if ((frmMain.GroupModuleRole == null || frmMain.GroupModuleRole.Count <= 0) && (frmMain.UserModuleRole == null || frmMain.UserModuleRole.Count <= 0))
                {
                    MessageControl.ShowMessage("Tài khoản hoặc mật khẩu không đúng", "Lỗi", MessageControl.Icon.Error);

                }
                else
                {
                    if (ckremember.Checked)
                    {
                        FileHelper.SetValueForXml(setingpath, "/setting/user", txtuser.Text);
                    }
                    if (checkEdit_EN.Checked)
                    {
                        FileHelper.SetValueForXml(setingpath, "/setting/language", "EN");
                    }
                    if (checkEdit_VN.Checked)
                    {
                        FileHelper.SetValueForXml(setingpath, "/setting/language", "VN");
                    }
                    this.DialogResult = System.Windows.Forms.DialogResult.Yes;
                }
            }
            catch
            {
                MessageControl.ShowMessage("Không thể kết nối tới máy chủ\r\nvui lòng kiểm tra lại đường truyền hoặc cấu hình của bạn", "Lỗi", MessageControl.Icon.Error);
            }
        
        }

        private void checkEdit_VN_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit_VN.Checked)
            {
                ValueDefine.Lang = ValueDefine.Language.VN;
                SetLanguage(ValueDefine.Language.VN);
                checkEdit_EN.Checked = false;
                DevExpress.XtraGrid.Localization.GridLocalizer.Active = new VietnameseGridLocalizer();
                DevExpress.XtraEditors.Controls.Localizer.Active = new VietnameseEditorsLocalizer();
            }
        }    

        private void checkEdit_EN_CheckedChanged(object sender, EventArgs e)
        {
            if (checkEdit_EN.Checked)
            {
                ValueDefine.Lang = ValueDefine.Language.EN;
                SetLanguage(ValueDefine.Language.EN);
                checkEdit_VN.Checked = false;
                DevExpress.XtraGrid.Localization.GridLocalizer.Active = new GridLocalizer();
                DevExpress.XtraEditors.Controls.Localizer.Active = new Localizer();
            }
        }

        void SetLanguage(ValueDefine.Language lang)
        {
            if (lang == ValueDefine.Language.VN)
            {
                this.lbllogin.Text = "ĐĂNG NHẬP";
                this.lblpass.Text = "Mật khẩu";
                this.lbluser.Text = "Tài khoản";
                this.lbldangnhap.Text = "Đăng nhập";
                this.lblhuy.Text = "Hủy";
                this.Text = "Đăng Nhập";
                this.ckremember.Text = "Nhớ tên đăng nhập";
            }
            else
            {
                this.lbllogin.Text = "LOGIN";
                this.lblpass.Text = "Password";
                this.lbluser.Text = "User Name";
                this.lbldangnhap.Text = "Login";
                this.lblhuy.Text = "Cancel";
                this.Text = "Login";
                this.ckremember.Text = "Remember user name";
            }
        }

        private void lblhuy_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
        }

        private void lbldangnhap_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void txtuser_MouseDown(object sender, MouseEventArgs e)
        {
            txtuser.BackColor = Color.MistyRose;
        }

        private void txtuser_MouseLeave(object sender, EventArgs e)
        {
            txtuser.BackColor = Color.White;
        }

        private void txtpass_MouseDown(object sender, MouseEventArgs e)
        {
            txtpass.BackColor = Color.MistyRose;
        }

        private void txtpass_MouseLeave(object sender, EventArgs e)
        {
            txtpass.BackColor = Color.White;
        }
        
    }
}
